// Copyright (C) 2016 All rights reserved.
// Author: liufeng11@letv.com (Liu Feng)
// Description:
#include "MyFontFeatureExtractor.h"

#define I(b) (b?1:0)
void MyFontFeatureExtractor::ExtractFeature(const unsigned char* data, std::vector<double>* feature) {
  if (!feature || !data) return;

  // Show(data, ROWS, COLS);

  std::vector<double>& v = *feature;
  v.resize((ROWS-2)*(COLS-2));

  for (int i = 1; i < ROWS-1; ++i) {
    for (int j = 1; j < COLS-1; ++j) {
      unsigned char center = P(data, i, j); 
      unsigned char lbpCode = 0;
      lbpCode |=  I(P(data, i-1, j-1) > center) << 7;
      lbpCode |=  I(P(data, i-1, j  ) > center) << 6;
      lbpCode |=  I(P(data, i-1, j+1) > center) << 5;
      lbpCode |=  I(P(data, i,   j+1) > center) << 4;
      lbpCode |=  I(P(data, i+1, j+1) > center) << 3;
      lbpCode |=  I(P(data, i+1, j  ) > center) << 2;
      lbpCode |=  I(P(data, i+1, j-1) > center) << 1;
      lbpCode |=  I(P(data, i  , j-1) > center) << 0;
    
      v[(i-1)*(ROWS-2)+(j-1)] = lbpCode;
    }
  }
  
}
